import React, { Component } from "react";
import { Toast } from "antd-mobile";
import MainLayout from "@/component/mainLayout/mainLayout";
import Scroll from "@/component/scrollLoading/index";
import yhTools from "@/tools/yh";
import "./pdfAgreement.scss";
import XHQBConfig from "@/config/XHQBConfig";
class PdfAgreement extends Component {
	constructor(props) {
		super(props);
		this.state = {
			imgs: [],
			pageIndex: 0,
			hasMore: true, //true:有更多数据，false:没有更多
			isRefresh: true
		};
	}
	componentDidMount() {
		//加载第一页数据
		const { state = {} } = this.props.location;
		let type = state.type;
		this.init(type);
	}
	handleUpRefresh = () => {
		const { type } = this.state;
		if (!this.state.hasMore) {
			return false;
		}
		Toast.loading("加载中...", 100);
		let objs = {
			url: XHQBConfig.agreement_xhfPrePDF,
			params: {
				pageIndex: this.state.pageIndex + 1,
				pageSize: 2,
				type
			},
			_alertShow: false
		};
		yhTools.request(objs).then(res => {
			Toast.hide();
			if (res.code === "0") {
				let newImgArr = [...this.state.imgs, ...res.data.imgs];
				if (newImgArr.length >= res.data.totalPage) {
					this.setState({ hasMore: false });
				} else {
					this.setState({ hasMore: true });
				}
				this.setState({
					pageIndex: this.state.pageIndex + 1,
					imgs: newImgArr
				});
				//添加完数据后刷新scroll,否则无法滑动
				this.child.parentRefresh();
			} else {
				this.setState({
					pageIndex: this.state.pageIndex - 1
				});
				Toast.info(res.message, 2);
				setTimeout(() => {
					this.props.history.goBack();
				}, 2000);
			}
		});
	};
	init(type) {
		Toast.loading("加载中...", 100);
		let objs = {
			url: XHQBConfig.agreement_xhfPrePDF,
			params: {
				pageIndex: 1,
				pageSize: 2,
				type
			},
			_alertShow: false
		};
		yhTools.request(objs).then(res => {
			Toast.hide();
			if (res.code === "0") {
				let newImgArr = [...this.state.imgs, ...res.data.imgs];
				this.setState({
					imgs: newImgArr,
					pageIndex: 1,
					type
				});
				//添加完数据后刷新scroll,否则无法滑动
				this.child.parentRefresh();
			} else {
				Toast.info(res.message, 2);
				setTimeout(() => {
					this.props.history.goBack();
				}, 2000);
			}
		});
	}
	render() {
		const { state = {} } = this.props.location;
		let name = state.name;
		const _pageParams = {
			header: {
				title: name
			},
			history: this.props.history,
			cacheLifecycles: this.props.cacheLifecycles
		};
		return (
			<MainLayout pageParams={_pageParams}>
				<div className="pdfAgreement-page-content" ref={e => (this.scroll = e)}>
					<Scroll
						onRef={ref => {
							this.child = ref;
						}}
						isRefresh={this.state.isRefresh}
						isScroll={true}
						hasMore={this.state.hasMore}
						pullUp={this.handleUpRefresh}
						{...this.props}
					>
						{this.state.imgs.map((item, index) => {
							return (
								<img
									className="ag-img"
									src={item.imgContent}
									alt=""
									key={index}
								/>
							);
						})}
					</Scroll>
					{/* {!this.state.hasMore && <p className="ag-footer">加载完成</p>}
					{this.state.isLoading && <p className="ag-footer">加载中...</p>} */}
				</div>
			</MainLayout>
		);
	}
}
export default PdfAgreement;
